package com.productivity.model;

import java.nio.FloatBuffer;

import com.productivity.model.animation.Animation;
import com.productivity.model.animation.AnimationListener;
import com.productivity.model.animation.AnimationType;
import com.productivity.model.animation.StandingAnimation;
import com.productivity.model.common.Direction;
import com.productivity.model.common.DrawType;
import com.productivity.model.common.Size3D;
import com.productivity.model.common.Vector3D;
import com.productivity.model.common.Vertex3D;

public class Player extends DynamicGameObject implements AnimationListener {
	
	private float[] verts = {0.00000f, -0.00000f, 0.00000f, 0.64952f, -0.37500f, 0.00000f, 0.64952f, -0.37500f, 0.00000f, 0.64952f, -1.12500f, 0.00000f, 0.00000f, -0.00000f, 0.00000f, -0.64952f, -0.37500f, 0.00000f, -0.64952f, -0.37500f, 0.00000f, -0.64952f, -1.12500f, 0.00000f, 0.00000f, -0.00000f, 0.00000f, 0.00000f, -1.00000f, 0.00000f, 0.00000f, -1.00000f, 0.00000f, -0.50000f, -1.86603f, 0.00000f, -0.50000f, -1.86603f, 0.00000f, -0.50000f, -2.86603f, 0.00000f, 0.00000f, -1.00000f, 0.00000f, 0.50000f, -1.86603f, 0.00000f, 0.50000f, -1.86603f, 0.00000f, 0.50000f, -2.86603f, 0.00000f};
	private float[] headVerts = {0.30000f,0.15000f,0.28532f,0.24271f,0.28532f,0.24271f,0.24271f,0.32634f,0.24271f,0.32634f,0.17634f,0.39271f,0.17634f,0.39271f,0.092705f,0.43532f,0.092705f,0.43532f,0,0.45000f,0,0.45000f,-0.092705f,0.43532f,-0.092705f,0.43532f,-0.17634f,0.39271f,-0.17634f,0.39271f,-0.24271f,0.32634f,-0.24271f,0.32634f,-0.28532f,0.24271f,-0.28532f,0.24271f,-0.30000f,0.15000f,0.30000f,0.15000f,0.28532f,0.057295f,0.28532f,0.057295f,0.24271f,-0.026336f,0.24271f,-0.026336f,0.17634f,-0.092705f,0.17634f,-0.092705f,0.092705f,-0.13532f,0.092705f,-0.13532f,0,-0.15000f,0,-0.15000f,-0.092705f,-0.13532f,-0.092705f,-0.13532f,-0.17634f,-0.092705f,-0.17634f,-0.092705f,-0.24271f,-0.026336f,-0.24271f,-0.026336f,-0.28532f,0.057295f,-0.28532f,0.057295f,-0.30000f,0.15000f};
	private Animation currAnimation;
	private float direction;
	private boolean moving;
	private float angVelocity;
	private FloatBuffer buffer;
	private PlayerActionListener listener;
	
	public Player(PlayerActionListener listener) {
		currAnimation = null;
		direction = 0;
		velocity = new Vertex3D();
		this.listener = listener;
		location = new Vertex3D(3,3f,0);
		vertices = new Vertex3D[18];
		buffer = OBJLoader.loadObjectFromFile("character.obj");
		size = new Size3D(2f,4f,0.5f);
		drawOffset = new Vertex3D(0,4.6f,0);
		
		for(int i = 0; i < vertices.length; i++)
			vertices[i] = new Vertex3D(verts[i*3],verts[i*3+1],verts[i*3+2]);
	}
	public void update(Map map) {

		direction+=angVelocity;
		location.setY(location.getY()+velocity.getY());
		if(moving) {
			location = location.addVector(new Vector3D(direction,0.1f));
		}
		if(currAnimation!=null) {
			vertices = currAnimation.getInterFrame();
			currAnimation.update();
		}
	}
	public float getDirection() {
		return direction;
	}
	public void move() {
		if(!moving) {
			moving = true;
		}
	}
	public void rotate(float angle) {
		angVelocity = angle;
	}
	public void stopRotate() {
		angVelocity = 0;
	}
	public void setDirection(float direction) {
		this.direction = direction;
	}
	public DrawType getDrawType() {
		return DrawType.LINE;
	}
	public void stopMove() {
		moving = false;
		if(currAnimation!=null)
			currAnimation.stopAnimation();
	}
	public void animationFinished(AnimationType type) {
		
	}
	
	@Override
	public Size3D getModelSize() {
		return new Size3D(12f,24f,3);
	}
	public FloatBuffer getVertexBuffer() {	
//		if(vertices==null) return null;
//		FloatBuffer buff = FloatBuffer.allocate(vertices.length*3);
//		for(int i = 0; i < vertices.length;i++)
//			buff.put(vertices[i].getArray());
//		buff.rewind();
		buffer.rewind();
		return buffer;
	}
	public FloatBuffer getNormalBuffer() {	
		buffer.rewind();
		return (FloatBuffer)buffer.position(3);
	}
}
